package com.itcast.tanhuaserver.controller;

import com.itcast.tanhuaserver.domain.AnswerTemplate;
import com.itcast.tanhuaserver.domain.Answers;
import com.itcast.tanhuaserver.service.SoulService;
import com.itcast.tanhuaserver.vo.SoulResultVo;
import com.itcast.tanhuaserver.vo.TestSoulVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/3/119:26
 * @Version 1.0
 */
@RestController
@RequestMapping("testSoul")
public class SoulController {

    @Autowired
    private SoulService soulService;


    /**
     * 查询问卷列表
     * @return
     */
    @GetMapping
    public ResponseEntity<List<TestSoulVo>> querySoulList1(){
        try {
            List<TestSoulVo> soulList = this.soulService.querySoulList();
            if (!CollectionUtils.isEmpty(soulList)){
                return ResponseEntity.ok(soulList);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询问卷列表
     * @return
     */
    //@GetMapping
    public ResponseEntity<List<TestSoulVo>> querySoulList2(){
        try {
            List<TestSoulVo> soulVoList = this.soulService.querySoulList1();
            if (!CollectionUtils.isEmpty(soulVoList)){
                return ResponseEntity.ok(soulVoList);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询问卷列表
     * @return
     */
    //@GetMapping
    public ResponseEntity<List<TestSoulVo>> querySoulList3(){
        try {
            List<TestSoulVo> soulList = this.soulService.querySoulList2();
            if (!CollectionUtils.isEmpty(soulList)){
                return ResponseEntity.ok(soulList);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 提交问卷
     * @param answerTemplate
     * @return
     */
    @PostMapping
    public ResponseEntity<String> importSoulTest(@RequestBody AnswerTemplate answerTemplate){
        try {
            String reportId = this.soulService.importSoulTest(answerTemplate.getAnswers());
             return ResponseEntity.ok(reportId);
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 返回结果
     * @param id
     * @return
     */
    @GetMapping("/report/{id}")
    public ResponseEntity<SoulResultVo> queryReportById(@PathVariable("id") String id){
        try {
            SoulResultVo soulResultVo = this.soulService.queryReportById(id);
            return ResponseEntity.ok(soulResultVo);
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 返回结果
     * @param id
     * @return
     */
    //@GetMapping("/report/{id}")
    public ResponseEntity<SoulResultVo> queryReportById1(@PathVariable("id") String id){
        try {
            SoulResultVo soulResultVo = this.soulService.queryReportById1(id);
            return ResponseEntity.ok(soulResultVo);
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
